# 密码管理数据模型
from models import db
from datetime import datetime

class Password(db.Model):
    """密码管理模型"""
    __tablename__ = 'passwords'
    
    id = db.Column(db.Integer, primary_key=True)
    website = db.Column(db.String(200), nullable=False)  # 网站/服务名称
    username = db.Column(db.String(100), nullable=False)  # 用户名/邮箱
    password_encrypted = db.Column(db.Text, nullable=False)  # 加密后的密码
    category = db.Column(db.String(50), default='其他')  # 分类: 邮箱, 银行, 社交等
    url = db.Column(db.String(500))  # 网站URL
    notes = db.Column(db.Text)  # 备注
    created_at = db.Column(db.DateTime, default=datetime.utcnow)  # 创建时间
    updated_at = db.Column(db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)  # 更新时间
    
    def to_dict(self, include_password=False):
        """转换为字典
        
        Args:
            include_password: 是否包含加密密码（默认不包含）
        """
        data = {
            'id': self.id,
            'website': self.website,
            'username': self.username,
            'category': self.category,
            'url': self.url,
            'notes': self.notes,
            'created_at': self.created_at.isoformat(),
            'updated_at': self.updated_at.isoformat()
        }
        if include_password:
            data['password_encrypted'] = self.password_encrypted
        return data
    
    def __repr__(self):
        return f'<Password {self.website}>'

class MasterPassword(db.Model):
    """主密码模型（用于加密所有密码）"""
    __tablename__ = 'master_password'
    
    id = db.Column(db.Integer, primary_key=True)
    password_hash = db.Column(db.String(200), nullable=False)  # 主密码的哈希值
    salt = db.Column(db.String(100), nullable=False)  # 盐值
    created_at = db.Column(db.DateTime, default=datetime.utcnow)  # 创建时间
    
    def __repr__(self):
        return f'<MasterPassword>'

